package patterns.behavioral.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 观察者模式示例
 * 定义对象间的一种一对多的依赖关系，当一个对象的状态发生改变时，所有依赖于它的对象都得到通知并被自动更新
 */
public class ObserverPattern {

    public static void main(String[] args) {
        // 推模型示例
        System.out.println("===== 推模型 =====");
        ConcreteSubject subject = new ConcreteSubject();
        
        Observer observer1 = new ConcreteObserver("观察者1");
        Observer observer2 = new ConcreteObserver("观察者2");
        
        subject.attach(observer1);
        subject.attach(observer2);
        
        subject.setState(10);
        subject.setState(20);
        
        subject.detach(observer1);
        subject.setState(30);
        
        // 拉模型示例
        System.out.println("\n===== 拉模型 =====");
        PullConcreteSubject pullSubject = new PullConcreteSubject();
        
        PullObserver pullObserver1 = new PullConcreteObserver("拉模型观察者1", pullSubject);
        PullObserver pullObserver2 = new PullConcreteObserver("拉模型观察者2", pullSubject);
        
        pullSubject.attach(pullObserver1);
        pullSubject.attach(pullObserver2);
        
        pullSubject.setState(100);
        pullSubject.setName("测试数据");
        pullSubject.notifyObservers(); // 手动通知
        
        // 实际应用示例：事件监听
        System.out.println("\n===== 事件监听示例 =====");
        Button button = new Button("登录按钮");
        
        button.addClickListener(new ClickListener() {
            @Override
            public void onClick(ClickEvent event) {
                System.out.println("处理点击事件: " + event.getSource().getName() + " 被点击了");
                System.out.println("执行登录逻辑...");
            }
        });
        
        button.click(); // 模拟按钮点击
    }
}

/**
 * 推模型实现
 */

// 主题接口
interface Subject {
    void attach(Observer observer);
    void detach(Observer observer);
    void notifyObservers();
}

// 观察者接口
interface Observer {
    void update(int state);
}

// 具体主题
class ConcreteSubject implements Subject {
    private List<Observer> observers = new ArrayList<>();
    private int state;
    
    public int getState() {
        return state;
    }
    
    public void setState(int state) {
        this.state = state;
        notifyObservers(); // 状态变化时通知观察者
    }
    
    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }
    
    @Override
    public void detach(Observer observer) {
        observers.remove(observer);
    }
    
    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(state); // 推模型：将状态推送给观察者
        }
    }
}

// 具体观察者
class ConcreteObserver implements Observer {
    private String name;
    private int state;
    
    public ConcreteObserver(String name) {
        this.name = name;
    }
    
    @Override
    public void update(int state) {
        this.state = state;
        System.out.println(name + " 收到更新，新状态为: " + state);
    }
}

/**
 * 拉模型实现
 */

// 拉模型主题接口
interface PullSubject {
    void attach(PullObserver observer);
    void detach(PullObserver observer);
    void notifyObservers();
}

// 拉模型观察者接口
interface PullObserver {
    void update();
}

// 拉模型具体主题
class PullConcreteSubject implements PullSubject {
    private List<PullObserver> observers = new ArrayList<>();
    private int state;
    private String name;
    
    public int getState() {
        return state;
    }
    
    public void setState(int state) {
        this.state = state;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public void attach(PullObserver observer) {
        observers.add(observer);
    }
    
    @Override
    public void detach(PullObserver observer) {
        observers.remove(observer);
    }
    
    @Override
    public void notifyObservers() {
        for (PullObserver observer : observers) {
            observer.update(); // 拉模型：只通知观察者，由观察者主动获取状态
        }
    }
}

// 拉模型具体观察者
class PullConcreteObserver implements PullObserver {
    private String name;
    private PullConcreteSubject subject;
    
    public PullConcreteObserver(String name, PullConcreteSubject subject) {
        this.name = name;
        this.subject = subject;
    }
    
    @Override
    public void update() {
        int state = subject.getState(); // 拉模型：观察者主动获取状态
        String subjectName = subject.getName();
        System.out.println(name + " 主动获取更新，状态为: " + state + ", 名称为: " + subjectName);
    }
}

/**
 * 实际应用示例：事件监听
 */

// 事件对象
class ClickEvent {
    private Button source;
    
    public ClickEvent(Button source) {
        this.source = source;
    }
    
    public Button getSource() {
        return source;
    }
}

// 事件监听器接口
interface ClickListener {
    void onClick(ClickEvent event);
}

// 按钮类（被观察者）
class Button {
    private String name;
    private List<ClickListener> listeners = new ArrayList<>();
    
    public Button(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    public void addClickListener(ClickListener listener) {
        listeners.add(listener);
    }
    
    public void removeClickListener(ClickListener listener) {
        listeners.remove(listener);
    }
    
    // 模拟按钮点击
    public void click() {
        System.out.println("按钮被点击: " + name);
        ClickEvent event = new ClickEvent(this);
        
        // 通知所有监听器
        for (ClickListener listener : listeners) {
            listener.onClick(event);
        }
    }
} 